package com.deuwise.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.system.entity.AppDo;
import com.deuwise.system.entity.SysMenu;
import com.deuwise.system.entity.SysRoleMenu;
import com.deuwise.system.service.AppDoService;
import com.deuwise.system.service.SysMenuService;
import com.deuwise.system.service.SysRoleMenuService;
import com.deuwise.system.mapper.SysRoleMenuMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Sysrolemenu 接口实现类
 *@author deuwise
 *@date 2018-11-06 14:15:04
 **/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    Logger logger= LoggerFactory.getLogger(SysRoleMenuServiceImpl.class);

    @Override
    public List<String> queryMenuIdList(String roleId) {
        Wrapper<SysRoleMenu> sysRoleMenuWrapper=new EntityWrapper<>();
        sysRoleMenuWrapper.eq("role_id",roleId);
        List<SysRoleMenu> list=this.selectList(sysRoleMenuWrapper);
        if(list!=null&&list.size()>0){
            return list.stream().map(f->{
                return  f.getMenuId();
            }).collect(Collectors.toList());
        }else{
            return  null;
        }

    }
    @Autowired
    private AppDoService appDoService;

    @Autowired
    private SysMenuService menuService;

    @Override
    public void saveOrUpdate(String roleId, List<String> menuIdList,String appId) {
       AppDo appDo= appDoService.selectById(appId);
       List<String> menus=new ArrayList<>();
        if(appDo!=null){
            Wrapper<SysMenu> doWrapper=new EntityWrapper<>();
            doWrapper.eq("app_id",appDo.getCode());
          List<SysMenu> menuList=menuService.selectList(doWrapper);
          if(menuIdList!=null&&menuIdList.size()>0){
              menus=menuList.stream().map(f->f.getId()).collect(Collectors.toList());
          }
        }
        //先删除角色与菜单关系
        Wrapper<SysRoleMenu> wrapper=new EntityWrapper<>();
        wrapper.eq("role_id",roleId);
        if(menus!=null&&menus.size()>0){
            wrapper.in("menu_id",menus);
        }
        baseMapper.delete(wrapper);

        if(menuIdList==null||menuIdList.size() == 0){
            return ;
        }

        //保存角色与菜单关系
        List<SysRoleMenu> list = new ArrayList<>(menuIdList.size());
        for(String menuId : menuIdList){
            SysRoleMenu sysRoleMenuEntity = new SysRoleMenu();
            sysRoleMenuEntity.setMenuId(menuId);
            sysRoleMenuEntity.setRoleId(roleId);

            list.add(sysRoleMenuEntity);
        }
        this.insertBatch(list);
    }
}